package com.community.web.master.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.model.entity.*;
import com.community.web.master.domain.dto.AddRoleMenuDto;
import com.community.web.master.domain.dto.PageRoleDto;
import com.community.web.master.domain.dto.RoleDto;
import com.community.web.master.domain.vo.MenuListWithSelectedVo;
import com.community.web.master.domain.vo.MenuVo;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.exception.DataException;
import com.community.web.master.mapper.MenuMapper;
import com.community.web.master.mapper.RoleMapper;
import com.community.web.master.mapper.RoleMenuMapper;
import com.community.web.master.mapper.RoleUserMapper;
import com.community.web.master.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private MenuMapper menuMapper;
    /**
     * 分页查询
     * @param pageRoleDto
     * @return
     */
    @Override
    public PageResult pageQuery(PageRoleDto pageRoleDto) {
        IPage<Role> page = new Page<Role>(pageRoleDto.getPage(),pageRoleDto.getPageSize());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
        if(pageRoleDto.getDescription()!=null && !pageRoleDto.getDescription().equals("")){
            queryWrapper.like("role_desc",pageRoleDto.getDescription());
        }
        roleMapper.selectPage(page,queryWrapper);

        return new PageResult(page.getTotal(),page.getRecords());
    }

    /**
     * 更新角色
     * @param roleDto
     */

    @Override
    public void updateUser(RoleDto roleDto) {
        Role role = roleMapper.selectById(roleDto.getId());
        if(role==null) throw new DataException("数据有误");
        BeanUtils.copyProperties(roleDto,role);
        roleMapper.updateById(role);
    }

    /**
     * 新增角色
     * @param roleDto
     */
    @Override
    public void addUser(RoleDto roleDto) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>().eq("role_name",roleDto.getRoleName());
        Role role = roleMapper.selectOne(queryWrapper);
        if (role!=null) throw new DataException("角色已存在");
        Role newRole = new Role();
        BeanUtils.copyProperties(roleDto,newRole);
        newRole.setId(null);
        roleMapper.insert(newRole);
    }

    /**
     * 赋予角色菜单权限
     * @param addRoleMenuDto
     */
    @Override
    @Transactional
    public void addMenu(AddRoleMenuDto addRoleMenuDto) {
        if(roleMapper.selectById(addRoleMenuDto.getId())==null) throw new DataException("角色不存在");
        List<Integer> selected = addRoleMenuDto.getSelected();
        QueryWrapper<RoleMenu> queryWrapper= new QueryWrapper<RoleMenu>().eq("role_id",addRoleMenuDto.getId());
        roleMenuMapper.delete(queryWrapper);
        for (Integer integer : selected) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(addRoleMenuDto.getId());
            roleMenu.setMenuId(integer);
            roleMenuMapper.insert(roleMenu);
        }
    }

    /**
     * 获取角色拥有的菜单
     * @param id
     * @return
     */
    @Override
    public MenuListWithSelectedVo getMenuMenuListWithSelected(Integer id) {
        //menuId获取
        List<RoleMenu> roleMenu = roleMenuMapper.selectList(new QueryWrapper<RoleMenu>().eq("role_id", id));
        List<Integer> menuids = roleMenu.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        //整个菜单的查询
        List<Menu> menuList = menuMapper.selectList(null);
        List<MenuVo> menuVoList = menuList.stream()
                .map(menu -> {
                    MenuVo menuVo = new MenuVo();
                    menuVo.setId(menu.getId());
                    menuVo.setMenuName(menu.getMenuName());
                    menuVo.setParentId(menu.getParentId());
                    // 设置其他属性...
                    return menuVo;
                })
                .collect(Collectors.toList());
        return new MenuListWithSelectedVo(menuVoList,menuids);
    }
}
